package socket.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;

/**
 * Created by admin
 * <p>
 * Date :2020/10/15
 * <p>
 * Description :
 * <p>
 * Version :1.0
 * 规定代码 ： 0加入游戏 ；1
 */
public class Client {
    private static final Logger logger = LoggerFactory.getLogger(Client.class);

    public static void main(String[] args) {
        try {
            connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void connect() throws IOException {
        Socket socket = new Socket("127.0.0.1", 10086);
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));


        BufferedReader clientReader = new BufferedReader(new InputStreamReader(System.in));
        String input =  clientReader.readLine();;
        String msg = null;
        while (!"exit".equals(input)){

            bufferedWriter.write(input);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            logger.info("client send : {}", input);
            msg = bufferedReader.readLine();
            if(msg != null){
                logger.info("server answer : {}", msg);
            }
            input = clientReader.readLine();
        }
        clientReader.close();
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();

    }

}
